package tag;

import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import operate.DictOperate;
import operate.vo.Type;
import util.StrUtil;
@SuppressWarnings("unchecked")
public class DictTypeShow extends TagSupport{
	private static final long serialVersionUID = 1L;
	private String defaultValue="";//
	private String selectName;//slect标签的名称同id属性值
	private String fun="";//js事件
	private String tip="";
	private String reg="";

	@Override
	public int doStartTag() throws JspException {
		HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
		JspWriter out = pageContext.getOut();
		try {
			out.write(getAllDictType());
		} catch (IOException e) {
			try {
				out.print("<font color=\"red\">标签出错</font>");
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
		return super.doStartTag();
	}
	public String  getAllDictType(){
		Map<String, Type>  map=DictOperate.getDictTypes();
		StringBuffer sb=new StringBuffer();
		if(StrUtil.exist(reg)){
			reg="reg='"+reg+"'";
		}
		sb.append("<select name='"+selectName+"' id='"+selectName+"' "+fun+" "+reg+">");
		Set set=map.entrySet();
		Iterator ite=set.iterator();
		if(!StrUtil.exist(defaultValue)){
			sb.append("<option value=''>请选择</option>");
		}
		while(ite.hasNext()){
			Map.Entry entry=(Map.Entry<String,Type>)ite.next();
			Type type=(Type) entry.getValue();
			sb.append("<option value='"+type.getCode()+"' ");
			if(StrUtil.exist(defaultValue)&&defaultValue.equals(type.getCode())){
				sb.append("selected='selected'");
			}
			sb.append(">"+type.getTypeName()+"</option>");
		}
		sb.append("</select>");
		return sb.toString();
	}
	public String getDefaultValue() {
		return defaultValue;
	}
	public void setDefaultValue(String defaultValue) {
		this.defaultValue = defaultValue;
	}
	public String getSelectName() {
		return selectName;
	}
	public void setSelectName(String selectName) {
		this.selectName = selectName;
	}
	public String getFun() {
		return fun;
	}
	public void setFun(String fun) {
		this.fun = fun;
	}
	public String getTip() {
		return tip;
	}
	public void setTip(String tip) {
		this.tip = tip;
	}
	public String getReg() {
		return reg;
	}
	public void setReg(String reg) {
		this.reg = reg;
	}
	public static long getSerialVersionUID() {
		return serialVersionUID;
	}
	
}
